package com.tidc.seaserverweb.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tidc.seaservermodel.dto.HistogramDto;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.Task;
import com.tidc.seaservermodel.entity.UserTask;
import com.tidc.seaservermodel.exception.TidcAuthorityException;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaserverservice.service.SupervisorTaskService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/supervisor/task")
//@CrossOrigin(origins = "*",maxAge = 3600,allowCredentials = "true")
public class SupervisorTaskController {
    @Resource
    private SupervisorTaskService taskService;

    @PostMapping("/add")
    public Fres<Integer> addTask(@RequestBody @Validated Task task, Authentication authentication) throws SQLException, TidcAuthorityException {
//        1日常2一次性 1主账号2次级账号
        Fres<Integer> fres = taskService.addTask(task, authentication);
        return fres;
    }
    @GetMapping("/all")
    public Fres<Map<Object, Object>> getTaskAll(@RequestParam("query")String query,@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum,Authentication authentication) throws TidcBadParent, TidcAuthorityException {
        return taskService.getTaskAll(query,pageSize, pageNum,authentication);
    }

    /**
     * 获取某个下级账号的待审核列表
     * @param id
     * @param pageSize
     * @param pageNum
     * @param authentication
     * @return
     */
    @GetMapping("/audit")
    public Fres<Map<Object, Object>> getAuditList(@RequestParam("id") int id,@RequestParam("pageSize") int pageSize, @RequestParam("pageNum") int pageNum,Authentication authentication) throws TidcBadParent, TidcAuthorityException {
        return taskService.getAuditList(id,pageSize,pageNum,authentication);
    }

    /**
     * 获取某个二级账号的N天内的成功任务和放弃任务次数
     *
     * @param day
     * @param id
     * @param authentication
     * @return
     */
    @GetMapping("/within/day")
    public Fres<List<HistogramDto>> getWithinTaskInfo(@Param("day") int day, @Param("id")int id, Authentication authentication){
        return  taskService.getWithinTaskInfo(day,id,authentication);
    }

    /**
     * 对审核列表的任务进行审核
     * @param userTask
     * @param authentication
     * @return
     */
    @PostMapping("/finish")
    public Fres<String> finishSecondaryTask(@RequestBody UserTask userTask, Authentication authentication) throws SQLException, TidcAuthorityException, JsonProcessingException {
        return taskService.finishSecondaryTask(userTask,authentication);
    }

    /**
     *
     * @param task 除了id都可以改
     * @param authentication
     * @return
     */
    @PutMapping("/put")
    public Fres<String> alterTask(@RequestBody Task task,Authentication authentication) throws SQLException, TidcAuthorityException {
        return taskService.alterTask(task,authentication);
    }

}
